package wq.gdky005.util.RASAndMD5;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringUtils {

	public static String converStreamToString(InputStream is) {

		if (is == null)
			return null;

		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuffer buffer = new StringBuffer();

		int count = 0;
		while (count < 3) {

			try {
				String line = null;
				while ((line = reader.readLine()) != null) {
					buffer.append(line);
				}
				break;
			} catch (IOException e) {
				count++;
			}
		}
		return buffer.toString();
	}

	public static String convertSecondsToYYMMDDString(long milliseconds) {
		SimpleDateFormat fn = new SimpleDateFormat("yyyy-MM-dd");
		String str = fn.format(new Date(milliseconds));
		return str;
	}

	public static String convertSecondsToYYYYString(long milliseconds) {
		SimpleDateFormat fn = new SimpleDateFormat("yyyy");
		String str = fn.format(new Date(milliseconds));
		return str;
	}

	public static String convertSecondsToMMDDString(long milliseconds) {
		SimpleDateFormat fn = new SimpleDateFormat("MM-dd");
		String str = fn.format(new Date(milliseconds));
		return str;
	}

	public static String convertSecondsToMMDDHHMMString(long milliseconds) {
		SimpleDateFormat fn = new SimpleDateFormat("MM-dd hh:mm");
		String str = fn.format(new Date(milliseconds));
		return str;
	}

	public static String convertSecondsToYYMMDDStringhhmm(long milliseconds) {
		SimpleDateFormat fn = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		String str = fn.format(new Date(milliseconds));
		return str;
	}

	public static long convertYYMMDDtoSeconds(String time) {
		String sDt = time;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date dt2 = null;
		long lTime = 0;
		try {
			dt2 = sdf.parse(sDt);
			lTime = dt2.getTime() / 1000;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return lTime;
	}

	public static String convert2Time(long start, long end) {
		long between = (end - start) / 1000;// 除以1000是为了转换成秒

		long day1 = between / (24 * 3600);
		long hour1 = between % (24 * 3600) / 3600;
		long minute1 = between % 3600 / 60;
		long second1 = between % 60 / 60;

		return ("" + day1 + "天" + hour1 + "小时" + minute1 + "分" + second1 + "秒");
	}

	/**
	 * 获取当前日期时间串,格式如2013-03-12 16:50
	 * 
	 * @return
	 */
	public static String getCurrentDateAndTime() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
	}

	public static Boolean isBlank(String str) {
		return false;
	}

	/*
	 * 功能：获取arr1中下标从startIndexInclusive到endIndexExclusive的子数组，不包括endIndexExclusive
	 */

	public static byte[] subarray(byte[] array, int startIndexInclusive,
			int endIndexExclusive) {
		if (array == null) {
			return null;
		}
		if (startIndexInclusive < 0) {
			startIndexInclusive = 0;
		}
		if (endIndexExclusive > array.length) {
			endIndexExclusive = array.length;
		}
		int newSize = endIndexExclusive - startIndexInclusive;
		Class type = array.getClass().getComponentType();
		if (newSize <= 0) {
			return (byte[]) Array.newInstance(type, 0);
		}
		byte[] subarray = (byte[]) Array.newInstance(type, newSize);
		System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
		return subarray;
	}

	/*
	 * 
	 * 功能：将arr2追加到arr1尾部：
	 */
	public static byte[] addAll(byte[] array1, byte[] array2) {
		if (array1 == null) {
			return array2;
		} else if (array2 == null) {
			return array1;
		}
		byte[] joinedArray = (byte[]) Array.newInstance(array1.getClass()
				.getComponentType(), array1.length + array2.length);
		System.arraycopy(array1, 0, joinedArray, 0, array1.length);
		System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
		return joinedArray;
	}

}
